package cc.rengu.igas.channel.cpcn.core.service.mchntorder;

import cc.rengu.igas.channel.cpcn.common.constant.CpcnParamConstant;
import cc.rengu.igas.channel.cpcn.common.constant.CpcnTreeNodeConstant;
import cc.rengu.igas.channel.cpcn.core.service.base.CpcnBaseService;
import cc.rengu.igas.channel.cpcn.facade.request.CpcnMchntOrderPayRefundRequest;
import cc.rengu.igas.channel.cpcn.facade.response.CpcnMchntOrderPayRefundResponse;
import cc.rengu.jradp.schedule.ScheduleImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

/**
 * @author wukefan
 * @version 1.0.0
 * @date 2020/7/22 14:08
 */
public class CpcnMchntOrderPayRefundService extends CpcnBaseService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        CpcnMchntOrderPayRefundRequest request = new CpcnMchntOrderPayRefundRequest();
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {

        XmlTreeUtil xmlTree = new XmlTreeUtil();
        CpcnMchntOrderPayRefundRequest cpcnMchntOrderPayRefundRequest = (CpcnMchntOrderPayRefundRequest) request;
        String institutionId = cpcnMchntOrderPayRefundRequest.getInstitutionId();
        String paymentNo = cpcnMchntOrderPayRefundRequest.getPaymentNo();
        BizResponse<CpcnMchntOrderPayRefundResponse> bizResponse = new BizResponse<>();
        CpcnMchntOrderPayRefundResponse response = new CpcnMchntOrderPayRefundResponse();
        if (StringUtil.isNullorEmpty(institutionId) ||
                StringUtil.isNullorEmpty(paymentNo)) {
            //失败赋值应答码为明确失败及应答消息直接退出
            rglog.error("缺少一个或多个业务参数！");
            response.setRespCode(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespCode());
            response.setRespDesc(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespDesc());
            bizResponse.setResult(response);
            return bizResponse;
        }
        rglog.trace("检查请求通过");

        /*将请求赋值到xml中*/
        requesToXMLTree(xmlTree, cpcnMchntOrderPayRefundRequest);

        /* TODO 演示环境默认返回成功 START */
        if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
            rglog.info("演示环境默认返回成功");
            response.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            response.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setResult(response);
            return bizResponse;
        }
        /* TODO 演示环境默认返回成功 END */

        try {
            int retCode = schedule.callService(CpcnParamConstant.CPCN_COMMUNICATION_SVC_NM, CpcnParamConstant.CPCN_COMMUNICATION_SVC_LABEL);
            /*判断retCode返回值*/
            /*通讯成功*/
            if (ScheduleImpl.SERVICE_SUCCESS == retCode) {
                /*判断状态码*/
                String respCode = xmlTree.getXmlTreeStringValue(CpcnTreeNodeConstant.RESP_CODE);
                if (CpcnParamConstant.CPCN_RESP_SUCC.equals(respCode)) {
                    rglog.debug("调用通道业务成功");
                    //通道调用成功的应答处理
                    try {
                        ConvertUtil.convertOutput(response);
                        //成功 转换成功应答码为统一应答码，调用方根据返回00000000判断业务是否成功
                        response.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                        response.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                        //金额转换必须放在convert后面
                    } catch (Exception e) {
                        //失败赋值应答码为明确失败及应答消息直接退出
                        rglog.error("{}", StringUtil.ExceptionToString(e));
                        response.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                        response.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                    }
                } else {
                    rglog.debug("调用通道业务失败");
                    String code = xmlTree.getXmlTreeStringValue(CpcnTreeNodeConstant.RESP_CODE);
                    if (StringUtil.isNullorEmpty(code)) {
                        response.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                    } else {
                        response.setRespCode(code);
                    }
                    String desc = xmlTree.getXmlTreeStringValue(CpcnTreeNodeConstant.RESP_DESC);
                    if (StringUtil.isNullorEmpty(desc)) {
                        response.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                    } else {
                        response.setRespDesc(desc);
                    }
                }
            } else if (ScheduleImpl.SERVICE_TIMEOUT == retCode) {
                //调用超时 赋值应答
                rglog.error("callService {} 失败！返回值={}", CpcnParamConstant.CPCN_COMMUNICATION_SVC_NM, retCode);
                response.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                response.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
                bizResponse.setResult(response);
                return bizResponse;
            } else {
                //调用明确失败
                rglog.error("callService {} 失败！返回值={}", CpcnParamConstant.CPCN_COMMUNICATION_SVC_NM, retCode);
                response.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
                response.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
                bizResponse.setResult(response);
                return bizResponse;
            }
        } catch (Exception e) {
            //失败赋值应答码为明确失败及应答消息直接退出
            rglog.error("{}", StringUtil.ExceptionToString(e));
            response.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
            response.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            bizResponse.setResult(response);
            return bizResponse;
        }
        bizResponse.setResult(response);
        return bizResponse;
    }

    private void requesToXMLTree(XmlTreeUtil xmlTree, CpcnMchntOrderPayRefundRequest cpcnMchntOrderPayRefundRequest) throws Exception {
        xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.TX_CODE, cpcnMchntOrderPayRefundRequest.getHeader().getTxnNum());
        xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.INSTITUTION_ID, cpcnMchntOrderPayRefundRequest.getInstitutionId());
        xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.PAYMENT_NO, cpcnMchntOrderPayRefundRequest.getPaymentNo());
        xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.SERIAL_NUMBER, cpcnMchntOrderPayRefundRequest.getSerialNumber());
        xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.AMOUNT, AmountUtil.changeY2F(cpcnMchntOrderPayRefundRequest.getAmount()));
        xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.ACCOUNT_TYPE, cpcnMchntOrderPayRefundRequest.getAccountType());
        if (!StringUtil.isNullorEmpty(cpcnMchntOrderPayRefundRequest.getRemark())) {
            xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.REMARK, cpcnMchntOrderPayRefundRequest.getRemark());
        }
        if (!StringUtil.isNullorEmpty(cpcnMchntOrderPayRefundRequest.getPaymentAccountName())) {
            xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.PAYMENT_ACCOUNT_NAME, cpcnMchntOrderPayRefundRequest.getPaymentAccountName());
        }
        if (!StringUtil.isNullorEmpty(cpcnMchntOrderPayRefundRequest.getPaymentAccountNumber())) {
            xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.PAYMENT_ACCOUNT_NUMBER, cpcnMchntOrderPayRefundRequest.getPaymentAccountNumber());
        }

        if (null != cpcnMchntOrderPayRefundRequest.getBankAccount()) {
            if (!StringUtil.isNullorEmpty(cpcnMchntOrderPayRefundRequest.getBankAccount().getBankId())) {
                xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.BANK_ACCTOUNT_BANK_ID, cpcnMchntOrderPayRefundRequest.getBankAccount().getBankId());
            }
            if (!StringUtil.isNullorEmpty(cpcnMchntOrderPayRefundRequest.getBankAccount().getAccountName())) {
                xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.BANK_ACCTOUNT_ACCT_NAME, cpcnMchntOrderPayRefundRequest.getBankAccount().getAccountName());
            }
            if (!StringUtil.isNullorEmpty(cpcnMchntOrderPayRefundRequest.getBankAccount().getAccountNumber())) {
                xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.BANK_ACCTOUNT_ACCT_NUMBER, cpcnMchntOrderPayRefundRequest.getBankAccount().getAccountNumber());
            }
            if (!StringUtil.isNullorEmpty(cpcnMchntOrderPayRefundRequest.getBankAccount().getBranchName())) {
                xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.BANK_ACCTOUNT_BRANCH_NAME, cpcnMchntOrderPayRefundRequest.getBankAccount().getBranchName());
            }
            if (!StringUtil.isNullorEmpty(cpcnMchntOrderPayRefundRequest.getBankAccount().getCity())) {
                xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.BANK_ACCTOUNT_CITY, cpcnMchntOrderPayRefundRequest.getBankAccount().getCity());
            }
            if (!StringUtil.isNullorEmpty(cpcnMchntOrderPayRefundRequest.getBankAccount().getProvince())) {
                xmlTree.setXmlTreeStringValue(CpcnTreeNodeConstant.BANK_ACCTOUNT_PROVINCE, cpcnMchntOrderPayRefundRequest.getBankAccount().getProvince());
            }
        }
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {
        CpcnMchntOrderPayRefundResponse cpcnMchntOrderPayRefundResponse = (CpcnMchntOrderPayRefundResponse) response.getResult();
        ConvertUtil.convertInput(cpcnMchntOrderPayRefundResponse);

    }
}
